package top.so.controller;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import top.so.dto.BlogCollectionDTO;
import top.so.dto.UserDTO;
import top.so.service.BlogCollectionService;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/collection")
@SessionAttributes("isSuccess")
public class CollectionController {
    @Autowired
    private BlogCollectionService blogCollectionService;

    @RequestMapping(value = "addCollection",method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public String addCollection(BlogCollectionDTO blogCollectionDTO,HttpSession httpSession){
        UserDTO userDTO = (UserDTO) httpSession.getAttribute("currUser");
        blogCollectionDTO.setUserID(userDTO.getUserID());
        String result = null;
        Map<String,Object> map = new HashMap<String,Object>();
        ObjectMapper mapper = new ObjectMapper();
        if(blogCollectionService.insertCollection(blogCollectionDTO)){
            map.put("collecBlog","true");
        }
        else{
            map.put("collecBlog","false");
        }
        try {
            result = mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "deleteCollection",method = RequestMethod.POST)
    public ModelAndView deleteCollection(BlogCollectionDTO blogCollectionDTO,HttpSession httpSession,ModelMap modelMap){

        ModelAndView mv = new ModelAndView("redirect:/myBlog/collection");
        UserDTO userDTO = (UserDTO) httpSession.getAttribute("currUser");
        blogCollectionDTO.setUserID(userDTO.getUserID());
        if(blogCollectionService.deleteCollection(blogCollectionDTO)){
            modelMap.addAttribute("isSuccess","true");
        }
        else{
            modelMap.addAttribute("isSuccess","false");
        }
        return mv;
    }
    @RequestMapping(value = "goPage",method = RequestMethod.POST)
    public ModelAndView goPage(int pageNum,HttpSession httpSession){
        UserDTO userDTO = (UserDTO) httpSession.getAttribute("currUser");
        ModelAndView mv = new ModelAndView("user/Collection");
        BlogCollectionDTO blogCollectionDTO = new BlogCollectionDTO();
        blogCollectionDTO.setUserID(userDTO.getUserID());
        List<BlogCollectionDTO> blogCollectionDTOList = blogCollectionService.selectWithPage(blogCollectionDTO,pageNum);
        if(!blogCollectionDTOList.isEmpty()){
            for(int i=0;i<blogCollectionDTOList.size();i++){
                if(blogCollectionDTOList.get(i).getArticleContent().length()>100){
                    blogCollectionDTOList.get(i).setArticleContent(blogCollectionDTOList.get(i).getArticleContent().replaceAll("\\r|\\n","").replace(" ","").substring(1,100));
                }
            }
            mv.addObject("blogCollectionDTOList",blogCollectionDTOList);
        }
        return mv;
    }

}
